#region Summary

// 
// Owner : JackieHan 
// 
// Solution : UpdateSystem
// 
// Project :Project
// 
// File : UpdateSystemInfo.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 更新服务器上的所有系统的总体信息
// 
// History : 
//			2011-01-05 10:20:58	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml;
using Jackie.Utility;

#endregion

namespace Skomart.Update.Config.Server
{
    /// <summary> 
    /// 更新服务器上的所有系统的总体信息
    /// </summary>
    public class UpdateServerInfo
    {
        /// <summary>
        /// 所有需更新的系统配置
        /// </summary>
        public IDictionary<string, UpdateSystemInfo> UpdateSysetms { get; set; }

        /// <summary>
        /// 根据系统名获取该系统的更新配置的索引
        /// </summary>
        /// <param name="systemName">系统名称</param>
        /// <returns></returns>
        public UpdateSystemInfo this[string systemName]
        {
            get
            {
                return UpdateSysetms[systemName];
            }
        }

        public UpdateServerInfo(string configDirectoryPath)
        {
            UpdateSysetms = new Dictionary<string, UpdateSystemInfo>();
            var configFilePath = Path.Combine(configDirectoryPath, "UpdateServerConfig.xml");

            if (!File.Exists(configFilePath))
                throw new FileNotFoundException("file not found!", configFilePath);

            var doc = new XmlDocument();
            doc.Load(configFilePath);

            var systemNodes = doc.GetElementsByTagName("System");
            foreach (var systemConfig in from XmlNode node in systemNodes select CreateSystemConfigInfo(node, configDirectoryPath))
            {
                UpdateSysetms.Add(systemConfig.SystemName, systemConfig);
            }
        }

        private UpdateSystemInfo CreateSystemConfigInfo(XmlNode node, string configDirectoryPath)
        {
            var systemName = node.Attributes["name"].Value;
            var configPath = Path.Combine(configDirectoryPath, node.Attributes["configFile"].Value);

            if (!File.Exists(configPath))
                throw new FileNotFoundException("file not found!", configPath);

            var doc = new XmlDocument();
            doc.Load(configPath);

            var publishNodes = doc.GetElementsByTagName("Publish");
            var publishLogs = (from XmlNode publishNode in publishNodes
                               select CreatePublishLogInfo(publishNode)).ToDictionary(publishLog => publishLog.SystemVersion);

            return new UpdateSystemInfo
                       {
                           SystemName = systemName,
                           PublishLogs = publishLogs
                       };
        }

        private PublishLogInfo CreatePublishLogInfo(XmlNode node)
        {
            var publishElement = node as XmlElement;
            if (publishElement == null)
                throw new FormatException("xmlNode to XmlElement Error");

            var fileNodes = publishElement.GetElementsByTagName("File");
            var files = (from XmlNode fileNode in fileNodes
                         select new UpdateFileInfo(fileNode)).ToList();

            var descriptionNodes = publishElement.GetElementsByTagName("Description");
            var descriptions = (from XmlNode descriptionNode in descriptionNodes
                                select descriptionNode.InnerText).ToList();

            var publishLogConfig = new PublishLogInfo
                                       {
                                           Date =
                                               JackieConvertor.ToDataTimeThrowException(
                                                   node.Attributes["date"].Value),
                                           Descriptions = descriptions,
                                           Files = files,
                                           SystemVersion = node.Attributes["systemVersion"].Value
                                       };
            return publishLogConfig;
        }
    }
}